package com.zp.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.zp.pojo.Books;
import com.zp.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author:周鹏
 * @date: 2020/12/7 17:32
 * @description:
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImp")
    private BookService bookService;

    @RequestMapping(value = "/allBook",produces = "application/jason;charset=utf-8")
    public String list(Model model) {
        List<Books> list = bookService.selectAll();
        for ( Books book:list
             ) {
            System.out.println(book);
        }
        model.addAttribute("list", list);
        return "allbook";
    }
    @RequestMapping("/getList")
    @ResponseBody
    public  List<Books> getbookList(){
        List<Books> list = bookService.selectAll();
        for (Books book: list
             ) {
            System.out.println(book);
        }
       return list;
    }
    @RequestMapping("/jason")
    @ResponseBody
    public String jason() throws JsonProcessingException {

        List<Books> list=new ArrayList<Books>();
        list.add(new Books(4,"种子植物",100,"植物额"));
        list.add(new Books(5,"种子植物",100,"植物额"));
        list.add(new Books(6,"种子植物",100,"植物额"));
        list.add(new Books(7,"种子植物",100,"植物额"));
        ObjectMapper objectMapper=new ObjectMapper();
//        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        objectMapper.setDateFormat(sdf);
//        Date date=new Date();
//        return objectMapper.writeValueAsString(date);
      return objectMapper.writeValueAsString(list);
    }
    @RequestMapping("/intercepeter")
    @ResponseBody
    public String MyIntercepetet(){
        System.out.println("====main方法执行=======");
         return "ok";
    }
}
